/**
 * Created by 97203 on 2018/06/08.
 */
$(function () {
    //1，将所用的变量都抽取出来，放在页面加载后
    var charge_datagrid = $("#charge_datagrid");
    var charge_dialog = $("#charge_dialog");
    var charge_form = $("#charge_form");
    var import_dialog = $("#import_dialog");
    charge_datagrid.datagrid({
        url: '/charge/query.do',
        fitColumns: true,
        fit: true,
        pagination: true,    //分页条
        rownumbers: true,     //行号列
        singleSelect: true,
        sortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: '#tb',
        onClickRow: function (index, row) {
            if (row.status) {
                $("#changeState_btn").linkbutton({
                    disabled: true
                })
            } else {
                $("#changeState_btn").linkbutton({
                    disabled: false
                })
            }

        },
        columns: [[
            {field: 'chargeSn', title: '保单号', width: 100},
            {field: 'client1', title: '投保人', width: 100,formatter: function (value,row) {
                return row.client.name ? row.client.name : "";
            }},
            {
                field: 'client2', title: '电话', width: 100,formatter: function (value,row) {
                return row.client.tel ? row.client.tel : "";
            }
            },
            {field: 'client3', title: '客户地址', width: 100,formatter: function (value,row) {
                return row.client.address ? row.client.address : "";
            }
            },
            {
                field: 'client4', title: '身份证号', width: 100,formatter: function (value,row) {
                return row.client.idNo ? row.client.idNo : "";
            }
            },
            {field:'sn',title:'核保单号',width:$(this).width()*0.1},
            {field: 'money', title: '保费金额', width: 100},
            {field: 'status', title: '缴费状态', width: 100,formatter: function (value) {
                return "<font color='green'>缴费完成</font>";
            }
            },
            {field: 'way', title: '缴费方式', width: 100,formatter: function (value) {
                if (value == 1){
                    return "现金支付";
                }else if(value == 2){
                    return "刷卡支付";
                }else if(value == 3){
                    return "支票支付";
                }else{
                    return "<font color='row'>异常</font>";
                }
            }},

            {field: 'startDate', title: '起保日期', width: 100},
            {field: 'endDate', title: '止保日期', width: 100},
            {field: 'organization', title: '保险机构', width: 100,formatter: function (value) {
                return value ? value.name : "";
            }
            },
            {
                field: 'employee', title: '录入人', width: 100,formatter: function (value) {
                return value ? value.username : "";
            }
            }
        ]],

        // onClickRow:function(){    //单击进行操作的方法
        //         var row = charge_datagrid.datagrid('getSelected');//获取选中行的数据
        //         console.log("7777");
        //         if (!row){
        //             return;//为防止意外情况可以选择加上此判断
        //         }
    })


    charge_dialog.dialog({
        width: 330,
        height: 500,
        buttons: '#btn',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            charge_form.form('clear');

            $("#a").textbox("setValue","险种");
            $("#b").textbox("setValue","险种金额");
            $("#c").textbox("setValue","已购产品列表");
        }
    })
    import_dialog.dialog({
        width: 400,
        height: 380,
        closed: true
    })

    //2，将所有方法同一交给一个对象管理
    var methodObj = {
        add: function () {
            //设置标题
            charge_dialog.dialog('setTitle', '新建任务单');
            //打开弹出框
            $("#password_tr").show();
            charge_dialog.dialog('open');
        },
        edit: function () {
            var row = charge_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要编辑的数据！', 'warning');
                return;
            }
            if (row.client) {
                row["client.id"] = row.client.id;
                row["client.name"] = row.client.name;
            }
            if (row.employee) {
                row["employee.id"] = row.employee.id;
                row["employee.username"] = row.employee.username;
            }
            if (row.product) {
                row["product.id"] = row.product.id;
                row["product.name"] = row.product.name;
                row["product.annuity"] = row.product.annuity;
            }

            if (row.organization) {
                row["organization.id"] = row.organization.id;
                row["organization.name"] = row.organization.name;
            }


            $.get('/charge/getRoleIdByEmpId.do', {chargeId: row.id}, function (data) {
                $("#role_combobox").combobox('setValues', data)
            })
            //把数据回显到表单中
            charge_form.form('load', row);
            //设置标题
            charge_dialog.dialog('setTitle', '信息资料');
            //打开弹出框
            charge_dialog.dialog('open');

        },
        changeState: function () {
            var row = charge_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
                if (r) {
                    $.get('/charge/changeState.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                charge_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', 'data.msg', 'warning');
                        }
                    })
                }
            });
        },
        changePwd: function () {
            var row = charge_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
                if (r) {
                    $.get('/charge/changePassword.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                charge_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', 'data.msg', 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            charge_datagrid.datagrid('reload');
        },
        save: function () {
            //提交ajax
            charge_form.form('submit', {
                url: '/charge/saveOrUpdate.do',

                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                            //关闭弹出框
                            methodObj.cancel();
                            //在dataGrid重新加载数据
                            charge_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', 'data.msg', 'warning');
                    }
                }
            })
        },
        remove: function () {
            var row = charge_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
                if(r){
                    $.get('/charge/delete.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                charge_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        cancel: function () {
            charge_dialog.dialog('close');
        },
        query: function () {
            var keyword = $("[name=keyword]").val();
            var chargeSn = $("[name=chargeSn]").val();
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val();
            charge_datagrid.datagrid('load', {
                keyword: keyword,
                chargeSn: chargeSn,
                beginTime: beginTime,
                endTime: endTime,
            })
        },
        exportXls: function () {
            var keyword = $("[name=keyword]").val();
            var chargeSn = $("[name=chargeSn]").val();
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val()
            window.location.href = "/charge/exportXls.do?keyword=" + keyword + "&chargeSn=" + chargeSn+"&beginTime="+beginTime+"&endTime="+endTime;
        },
        importXls: function () {
            import_dialog.dialog('open');
        },
    }
    //3，给按钮统一绑定点击事件
    $("a[data-cmd]").click(function () {
        //获取按钮上的data-cmd的值
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})
function fileSubmit() {
    var fileSize = $("#file").val();
    if(fileSize!=null && fileSize != ''){
        console.log("bu");
        $("#import_form").submit();
    }else {
        $.messager.alert('温馨提示', '请选中要上传的文件！', 'warning');
    }
}
